package core;

/**
 * Represents a block of text in the format:
 * 	[element attributes...]
 * 	text
 * 
 * @author john
 *
 */
public class WTagBlock {
	private String element, attribute, text;
	
	public WTagBlock(String tag) {
		tag = tag.trim();	// trim whitespace, so we won't ever encounter a space at the end
		int sp = tag.indexOf(' '); 		// find the first space
		if (sp != -1) {		// if space was found, it must be between at least 2 words
			element = tag.substring(0, sp);
			attribute = tag.substring(sp+1);
		}
		else {
			element = tag;
			attribute = "";
		}
		text = "";
	}
	
	public void setText(String t) {
		text = t.trim();
	}
	
	/**
	 * Gets the first word in the block header tag:
	 *   &nbsp;[<b>element</b> <i>attribute1 attribute2...</i>]
	 *   &nbsp;<i>text</i>
	 * @return element string of tag
	 */
	public String getElement() {
		return element;
	}
	/**
	 * Gets the rest of the words in the block header tag:
	 *   &nbsp;[element <b><i>attribute1 attribute2...</i></b>]
	 *   &nbsp;<i>text</i>
	 * @return attribute string of tag
	 */
	public String getAttribute() {
		return attribute;
	}
	/**
	 * Gets the text following the block header tag:
	 *   &nbsp;[element <i>attribute1 attribute2...</i>]
	 *   &nbsp;<b><i>text</i></b>
	 * @return text string of block
	 */
	public String getText() {
		return text;
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder(element.length() + attribute.length() + text.length() + 4);
		sb.append('[');
		sb.append(element);
		sb.append(attribute.isEmpty() ? "" : " ");
		sb.append(attribute);
		sb.append(']');
		sb.append(text.isEmpty() ? "" : "\n");
		sb.append(text);
		
		return sb.toString();
	}
}
